package org.game.msgBuilder;

import org.game.db.bean.DungeonLevel;
import org.game.db.bean.DungeonLevelGroup;
import org.game.db.bean.Player;
import org.game.module.fight.AbstractFight;
import org.game.protobuf.s2c.S2CDungeonMsg;
import org.game.protobuf.s2c.S2CHeroMsg;
import org.game.protobuf.s2c.S2CPlayerMsg;
import org.game.protobuf.s2c.S2CShareMsg;
import org.game.protobuf.s2c.S2CShareMsg.RewardsMsg;
import org.utils.timeframe.TimeFrameChecker;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;

/**
 * Created by fxf on 2017/8/30.
 */
public class DungeonMsgBuilder {

    public static S2CDungeonMsg.FightStartMsg getFightStartMsg(int levelCid, AbstractFight fight) {
        S2CDungeonMsg.FightStartMsg.Builder builder = S2CDungeonMsg.FightStartMsg.newBuilder();
        builder.setLevelCid(levelCid);
        
        if (fight.getHelpHero() != null) {
            builder.setHero(fight.getHelpHero());
        }
        
        builder.setFightId(fight.getFightId());
        
        // 随机种子
        builder.setRandomSeed(fight.getRandomSeed());
        
        // 奖励消息
 		List<RewardsMsg> rewards = Lists.newArrayList();
 		fight.getRewards().forEach((k,v) -> {
 			rewards.add(RewardsMsg.newBuilder().setId(k).setNum(v).build());
 		});
 		builder.addAllRewards(rewards);
 		
        return builder.build();
    }

    /**
     * 获取副本记录信息
     *
     * @param player 玩家对象
     */
    public static S2CDungeonMsg.GetLevelInfo getDungeonRecordMsg(Player player) {
        S2CDungeonMsg.GetLevelInfo.Builder builder = S2CDungeonMsg.GetLevelInfo.newBuilder();
        List<DungeonLevel> dungeonLevelList = player.getDungeonLevelsFkPlayerId();
        if (dungeonLevelList != null && dungeonLevelList.size() > 0) {
            builder.setLevelInfos(getLevelInfoList(dungeonLevelList));
        }
        List<DungeonLevelGroup> dungeonLevelGroupList = player.getDungeonLevelGroupsFkPlayerId();
        S2CDungeonMsg.RefreshDungeonLevelGroupList.Builder groupListBuilder = S2CDungeonMsg.RefreshDungeonLevelGroupList.newBuilder();
        dungeonLevelGroupList.forEach(group -> {
            groupListBuilder.addGroup(getDungeonLevelGroupInfo(group));
        });
        builder.setGroups(groupListBuilder);
        return builder.build();
    }

    public static S2CDungeonMsg.LevelInfos getLevelInfoList(List<DungeonLevel> levelList){
        S2CDungeonMsg.LevelInfos.Builder builder = S2CDungeonMsg.LevelInfos.newBuilder();
        levelList.forEach(level->{
            builder.addLevelInfos(getLevelInfo(level));
        });
        return builder.build();
    }

    public static S2CDungeonMsg.LevelInfo getLevelInfo(DungeonLevel level) {
        S2CDungeonMsg.LevelInfo.Builder levelInfoBuilder = S2CDungeonMsg.LevelInfo.newBuilder();
        levelInfoBuilder.setCid(level.getCid())
                .setWin(level.isWin())
                .setFightCount(level.getFightCount());
        levelInfoBuilder.addAllGoals(level.getAchieveGoals());
        return levelInfoBuilder.build();
    }

    /**
     * 获取战斗失败消息
     */
    public static S2CDungeonMsg.FightOverMsg getFightFailMsg(DungeonLevel level) {
        S2CDungeonMsg.FightOverMsg.Builder builder = S2CDungeonMsg.FightOverMsg.newBuilder();
        builder.setLevelInfo(getLevelInfo(level))
                .setWin(false);
        return builder.build();
    }

    /**
     * 获取战斗胜利消息
     */
    public static S2CDungeonMsg.FightOverMsg getFightWinMsg(DungeonLevel level, List<S2CShareMsg.RewardsMsg> rewardsMsgs) {
        S2CDungeonMsg.FightOverMsg.Builder builder = S2CDungeonMsg.FightOverMsg.newBuilder();
        builder.setWin(true)
                .setLevelInfo(getLevelInfo(level));
        if (rewardsMsgs != null && rewardsMsgs.size() > 0) {
            builder.addAllRewards(rewardsMsgs);
        }
        return builder.build();
    }

    public static S2CDungeonMsg.RefreshDungeonLevelGroupList getDungeonLevelGroupList(List<DungeonLevelGroup> groupList) {
        S2CDungeonMsg.RefreshDungeonLevelGroupList.Builder builder = S2CDungeonMsg.RefreshDungeonLevelGroupList.newBuilder();
        groupList.forEach(group -> {
            builder.addGroup(getDungeonLevelGroupInfo(group));
        });
        return builder.build();
    }

    public static S2CDungeonMsg.DungeonLevelGroupInfo getDungeonLevelGroupInfo(DungeonLevelGroup levelGroup) {
        S2CDungeonMsg.DungeonLevelGroupInfo.Builder builder = S2CDungeonMsg.DungeonLevelGroupInfo.newBuilder();
        builder.setId(levelGroup.getId() + "")
                .setCid(levelGroup.getCid())
                .setFightCount(levelGroup.getFightCount())
                .setBuyCount(levelGroup.getBuyCount())
                .setMainLineCid(levelGroup.getLastMainLine())
                .setMaxMainLine(levelGroup.getMaxMainLine());
        levelGroup.getGetedReward().forEach((difficulty, list) -> {
            builder.addRewardInfo(ShareMsgBuilder.createListMap((Integer) difficulty, (List<Integer>) list));
        });
        return builder.build();
    }

    /**
     * 获取活动副本更新信息
     */
    public static S2CDungeonMsg.UpdateActivityDungeon getUpdateActivityDungeonMsg(Map<Integer, List<Integer>> map) {
        S2CDungeonMsg.UpdateActivityDungeon.Builder builder = S2CDungeonMsg.UpdateActivityDungeon.newBuilder();
        map.forEach((type, list) -> {
            if (type == TimeFrameChecker.TIME_POINT_BEGIN) {
                builder.addAllStartIds(list);
            }
            if (type == TimeFrameChecker.TIME_POINT_END) {
                builder.addAllEndIds(list);
            }
        });
        return builder.build();
    }

    public static S2CDungeonMsg.GetLevelGroupReward getLevelGroupReward(int cid, int difficulty, int starNum, DungeonLevelGroup levelGroup) {
        S2CDungeonMsg.GetLevelGroupReward.Builder builder = S2CDungeonMsg.GetLevelGroupReward.newBuilder();
        builder.setCid(cid)
                .setDifficulty(difficulty)
                .setStarNum(starNum);
        levelGroup.getGetedReward().forEach((diff, list) ->
                builder.addRewardInfo(ShareMsgBuilder.createListMap((Integer) diff, (List<Integer>) list)));

        return builder.build();
    }

    public static S2CDungeonMsg.BuyFightCount buildBuyFightCountMsg(int cid){
        S2CDungeonMsg.BuyFightCount.Builder builder = S2CDungeonMsg.BuyFightCount.newBuilder();
        builder.setCid(cid);
        return builder.build();
    }

    public static S2CDungeonMsg.UpdateLevelGroupInfo getUpdateLevelGroupInfo(DungeonLevelGroup group){
        S2CDungeonMsg.UpdateLevelGroupInfo.Builder builder = S2CDungeonMsg.UpdateLevelGroupInfo.newBuilder();
        builder.setGroup(getDungeonLevelGroupInfo(group));
        return builder.build();
    }

    public static S2CDungeonMsg.ProgressMsg getProgressMsg(DungeonLevelGroup group) {
        S2CDungeonMsg.ProgressMsg.Builder builder = S2CDungeonMsg.ProgressMsg.newBuilder();
        builder.setGroup(getDungeonLevelGroupInfo(group));
        return builder.build();
    }

}
